package com.sakura.mall.coupon.homeAdv.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.homeAdv.domain.entity.HomeAdvEntity;
import com.sakura.mall.coupon.homeAdv.domain.request.AddHomeAdvRequest;
import com.sakura.mall.coupon.homeAdv.domain.request.UpdateHomeAdvRequest;
import com.sakura.mall.coupon.homeAdv.domain.request.HomeAdvPageRequest;
import com.sakura.mall.coupon.homeAdv.domain.vo.HomeAdvVo;
import com.sakura.mall.coupon.homeAdv.mapper.HomeAdvMapper;
import com.sakura.mall.coupon.homeAdv.service.HomeAdvService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 首页轮播广告 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class HomeAdvServiceImpl extends ServiceImpl<HomeAdvMapper, HomeAdvEntity> implements HomeAdvService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddHomeAdvRequest request) {
        // todo 其他校验逻辑
        HomeAdvEntity homeAdv = request.convert(HomeAdvEntity.class);
        this.save(homeAdv);
    }

    @Override
    public Page<HomeAdvVo> queryPage(HomeAdvPageRequest request) {
        IPage<HomeAdvEntity> qp = request.toPage();
        LambdaQueryWrapper<HomeAdvEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<HomeAdvVo> ret = qp.convert(entity -> entity.convert(HomeAdvVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateHomeAdvRequest request) {
        HomeAdvEntity entity = request.convert(HomeAdvEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

